﻿/*******************************************************
 *  药方打印管理 相关操作js
 * <p>Title: printManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年12月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//药方打印管理
var options, unitzTree, selNode,
    $table, addOrEditUrl, orgId = null, isParent = false, folder_path, issetOut = false;
var addIndex;
var printManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        printManager.bindZtree();
        printManager.initSelect();
        printManager.initDate();
        printManager.initTable();
        printManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化医院下拉
    initSelect: function () {
        // 初始化医院下拉
        $.ajax({
            url: ctx + '/BaneHospital/GetHospitalDataByPer',
            type: "post",
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data.Statu != 0)
                    return false;
                var itemStr = "<option value=''>空</option>";
                $.each(data.Data, function (i, item) {
                    itemStr += "<option value='" + item.hos_id + "'>" + item.hos_name + "</option>";
                });
                $('#hos_id').append(itemStr);
                $('#hos_id').selectpicker('refresh');
            }
            //,
            //error: function () {
            //    layer.msg('数据异常~', { icon: 5 });
            //}
        });
    },
    //初始化组织机构树
    bindZtree: function () {
        var setting = {
            view: {
                showIcon: true,
                selectedMulti: false,
                fontCss: getFontCss
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            async: {
                enable: true,
                dataType: "text",
                type: "post",
                url: ctx + '/SysOrg/GetOrgTreeDataByRelation',
                autoParam: ["id"]
            },
            data: {
                key: { title: "name", name: "name" },
                simpleData: { enable: true, idKey: "id", pIdKey: "pId", rootPId: 0 }
            },
            check: {
                enable: false,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "p", "N": "ps" }
            },
            callback: {
                onAsyncError: function () {
                    layer.msg("初始化单位结构树失败~", { icon: 5 });
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                    //树加载完成后执行
                    var obj = unitzTree.getNodes()[0];
                    if (obj) {
                        issetOut = true;
                        unitzTree.selectNode(obj);
                        unitzTree.setting.callback.onClick(null, unitzTree.setting.treeId, obj);//模拟点击事件 
                        if (obj.children && obj.children.length > 0)
                            unitzTree.expandNode(obj);
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    $('#unitTree a').css("color", "#333");
                    $('#' + treeNode.tId + '_a').css("color", "#ffffff");
                    isParent = treeNode.isParent;
                    orgId = treeNode.id;
                    selNode = treeNode;//选中节点
                    folder_path = treeNode.folder_path;
                    if (issetOut)
                        setTimeout(function () { $table.bootstrapTable('refresh', options); issetOut = false; }, 2000); 
                    else
                        setTimeout(function () { $table.bootstrapTable('refresh', options); }, 2000);
                }
            }
        };
        unitzTree = $("#unitTree").initzTreeView(setting, 'ushow');
    },
    //初始化日期
    initDate: function () {
        var newDate=new Date();
        var year = newDate.getFullYear().toString(), month = (newDate.getMonth() + 1).toString(), day = (newDate.getDate()).toString();
        if (month.length === 1)
            month = "0" + month;
        if (day.length === 1)
            day = "0" + day;
        var startDate = year + "-" + month +"-"+ day;
        var dateStart = {
            elem: '#dateStart',
            format: 'YYYY-MM-DD',
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false
        };
        laydate(dateStart);
        $('#dateStart').val(startDate);
    },
    //初始化Table
    initTable: function () {
        options = {
            url: ctx + "/BaneHospital/GetBaneRecipeData",
            cutHeight: 5,
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                var hos_id = $('#hos_id').val();
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    orgId: orgId,//选中单位(组织机构)
                    folder_path: folder_path,
                    hos_id: (hos_id) ? hos_id : 0,
                    start_date: $('#dateStart').val(),
                    printType:$('#printType').val(),
                    keyword: encodeURI($('#keyword').val())//人员姓名
                }
                return params;
            },
            columns: [
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        }, {
	            field: 'is_print',
	            title: '操作',
	            align: 'center',
	            formatter: function (value, row, index) {
                    return "<button class='btn btn-danger btn-sm btn-rounded btn-outline' onclick=printManager.openRecordPage('" + row.hos_name + "'," + row.ck_id + "," + row.user_id + ",'" + row.ck_date.substring(0, 10) + "')>打印</button>";
	                //if (value === 0)
	                //    return "<button class='btn btn-danger btn-sm btn-rounded btn-outline' onclick=printManager.openRecordPage('" + row.hos_name + "'," + row.ck_id + "," + row.user_id + ",'" + row.ck_date.substring(0, 10) + "')>打印</button>";
	                //return "";
	            }
	        },{
	            field: 'user_name',
	            title: '人员姓名',
	            align: 'center'
	        },{
	            field: 'user_sex',
	            title: '性别',
	            align: 'center'
	        },{
	            field: 'user_identify',
	            title: '身份证',
	            align: 'center'
	        },{
	            field: 'last_dose_num',
	            title: '上次检测剂量',
	            align: 'center'
	        },{
	            field: 'last_ck_date',
	            title: '上次检测时间',
	            align: 'center'
	        },{
	            field: 'hos_name',
	            title: '所属医院',
	            align: 'center'
	        },{
	            field: 'ck_date',
	            title: '检测时间',
	            align: 'center'
	        }]
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //打开考勤详情
    openRecordPage: function (hosName, ckId, userId, ckDate) {
        //1.0 获取考勤时间区间 hosRecordDetial
        var index = layer.open({
            id: "ifreamprintUser",
            title: ['打印处方笺', 'font-size:18px;'],
            type: 2,
            content: ctx + '/BaneHospital/hosPrintPage?hosName='+encodeURI(hosName)+'&userId=' + userId + '&ckDate=' + ckDate,
            scroll: true,//是否显示滚动条、默认不显示
            btn: ['打印'],
            area: ['620px', '450px'],
            yes: function (li, o) {               
                if (!!window.ActiveXObject || "ActiveXObject" in window) {
                    try {
                        var HKEY_Root, HKEY_Path, HKEY_Key;
                        HKEY_Root = "HKEY_CURRENT_USER";
                        HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
                        var Wsh=new ActiveXObject("WScript.Shell"); 
                        HKEY_Key="header"; 
                        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 
                        HKEY_Key="footer"; 
                        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
                    } catch (e) {
                        //alert("error:" + e);
                    }
                }
                var ifameId = $('#ifreamprintUser iframe');
                window.frames[ifameId.attr('id')].focus();
                window.frames[ifameId.attr('id')].print();
                //修改已打印
                $.ajax({
                    url: ctx + '/BaneHospital/ModifyPrintType/' + ckId,
                    type: "post",
                    async: false,
                    dataType: 'json',
                    success: function (data) {
                        if (data.Statu != 0) {
                            layer.msg(data.Msg, { icon: 5 });
                            return false;
                        }                          
                        $table.bootstrapTable('refresh', options);
                    }
                    //,
                    //error: function () {
                    //    layer.msg('数据异常~', { icon: 5 });
                    //}
                });
            }
        });
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            $table.bootstrapTable('refresh', options);
        });
    }
}


